﻿/**
 * Author: Bui Dac Bang
 */
package app.controller
{		
	import app.model.proxy.AssetProxy;
	import app.model.proxy.ConfigProxy;
	import app.model.proxy.UserProxy;
	import app.model.proxy.AppProxy;
	import app.model.proxy.ExternalProxy;
	import app.model.proxy.ConnectProxy;
	
	import app.model.vo.CookieVO;
	import app.model.vo.VariableVO;
	import app.view.component.moment.Lang;
	import app.view.component.moment.Module;
	import com.adobe.air.crypto.EncryptionKeyGenerator;
	import com.hutra.utils.Syner;
	import flash.display.Stage;
	import flash.system.Security;
	import org.osflash.thunderbolt.Logger;
	
    import org.puremvc.as3.interfaces.ICommand;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.command.SimpleCommand;
    
    import app.facade.App;	
	import app.facade.EApp;		
	import flash.display.Stage;
	import app.view.mediator.StageMediator;    
	
    public class VariableCommand extends SimpleCommand implements ICommand{
	
        override public function execute( note:INotification ) : void {
			facade.registerMediator( new StageMediator( (note.getBody() as Stage) ) );
			var body:Stage = note.getBody() as Stage;		
			
			//[1]
			var variable:VariableVO = Syner.synchronizeValueObject(new VariableVO(), body.loaderInfo.parameters);
			//[2]
			App.setShared(variable.cookie);
			var cookievo:CookieVO = Syner.synchronizeValueObject(new CookieVO(), App.getCookies());
			//[3]
			App.LANG			= variable.lang;
			App.VERSION			= variable.version;
			App.PROFILER		= variable.profiler;
			App.USERNAME 		= variable.username;
			App.USERID 			= variable.userid;
			App.CONFIG_LINK		= variable.config;
			
			App.dic	 			= Lang.a.getDictionary(variable.lang);
			
			//[1]
			var app:AppProxy 				= new AppProxy();
			var exter:ExternalProxy 		= new ExternalProxy();
			var config:ConfigProxy 			= new ConfigProxy();
			var asset:AssetProxy 			= new AssetProxy();
			var wive:UserProxy				= new UserProxy();
			var cont:ConnectProxy			= new ConnectProxy();
			
			facade.registerProxy(exter);
			facade.registerProxy(config);
			facade.registerProxy(asset);
			facade.registerProxy(app);
			facade.registerProxy(wive);
			facade.registerProxy(cont);
			
			//[2]
			app.setVariable(variable);
			
			//[3] @App.LOADING_PROXY_ALL -> LoadingCommand
			//app.loadingProxy(AssetProxy.NAME);
			//app.loadingProxy(AppProxy.NAME);
			app.loadingProxy(ConfigProxy.NAME);
			//app.loadingProxy(UserProxy.NAME);
			
			//[4]
			sendNotification(App.INIT_LOAD_DATA_PROXY);
			
			//[5]
			asset.load();
        }
    }
}